﻿using GPSTracker.API.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Linq;

namespace GPSTracker.API.DAL
{
    public class DeliveryDAL
    {
        public static List<DataModel.SP_GetReadyDelivery_RESTResult> GetReadyDeliveryData(DataModel.ClientData cd)
        {
            try
            {
                var login = LoginDAL.CheckLoginByTempo(cd.LoginName, cd.LoginPwd, "GetReadyDeliveryData");

                if (login != null)
                {
                    var context = new DataModel.GpsDCDataContext(CommonFx.GetConStr());
                    return context.SP_GetReadyDelivery_REST(login.TempoID).ToList();
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                var el = new DataModel.ErrorLog();
                el.ErrorMessage = ex.Message;
                el.ErrorForm = "DeliveryDAL";
                el.ErrorFunction = "GetReadyDeliveryData";
                CommonDAL.AddErrorLog(el);

                return null;
            }
        }

        public static bool SaveDeliveryStatus(DataModel.ClientData cd, List<DataModel.ClientData> listCD)
        {
            try
            {
                var login = LoginDAL.CheckLoginByTempo(cd.LoginName, cd.LoginPwd, "SaveDeliveryStatus");

                if (login != null)
                {
                    var context = new DataModel.GpsDCDataContext(CommonFx.GetConStr());

                    var settings = new XmlWriterSettings();
                    settings.NewLineHandling = NewLineHandling.Entitize;
                    settings.Indent = true;
                    settings.OmitXmlDeclaration = true;

                    var sb = new StringBuilder();

                    using (var xw = XmlWriter.Create(sb, settings))
                    {
                        xw.WriteStartDocument();
                        xw.WriteStartElement("DT");
                        xw.WriteStartElement("DE");

                        xw.WriteElementString("DeID", cd.DeliveryEntryID.ToString());
                        xw.WriteElementString("IsDel", cd.IsDelivered.ToString());

                        if (cd.IsDelivered == false)
                        {
                            xw.WriteElementString("RTO", cd.RTO);
                        }

                        foreach (var cd1 in listCD)
                        {
                            xw.WriteStartElement("DEF");                            
                            xw.WriteElementString("Question", cd1.Question);
                            xw.WriteElementString("Answer", cd1.Answer);
                            xw.WriteEndElement();
                        }

                        xw.WriteEndElement();
                        xw.WriteEndDocument();
                        xw.Close();
                    }

                    //System.Nullable<int> p1 = null;
                    bool? IsErrorFound = true;
                    string ErrorMessage = String.Empty;

                    byte[] CustSign = Convert.FromBase64String(cd.CustSign);

                    try
                    {
                        byte[] CustImg = Convert.FromBase64String(cd.CustImg);
                        context.SP_UpdateDeliveryStatus_REST(XElement.Parse(sb.ToString()), CustImg, CustSign, "D1", ref IsErrorFound, ref ErrorMessage);
                    }
                    catch
                    {
                        context.SP_UpdateDeliveryStatus_REST(XElement.Parse(sb.ToString()), null, CustSign, "D1", ref IsErrorFound, ref ErrorMessage);
                    }

                    if (IsErrorFound == false)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                var el = new DataModel.ErrorLog();
                el.ErrorMessage = ex.Message;
                el.ErrorForm = "DeliveryDAL";
                el.ErrorFunction = "SaveDeliveryStatus";
                CommonDAL.AddErrorLog(el);

                return false;
            }
        }

        private static byte[] StreamToBytes(Stream stream)
        {
            byte[] output = new byte[0];
            byte[] stream_array = new byte[0];
            byte[] buffer = new byte[1024];

            int read;
            while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                stream_array = new byte[output.Length + read];
                output.CopyTo(stream_array, 0);
                Array.Copy(buffer, 0, stream_array, output.Length, read);
                output = stream_array;
            }

            return output;
        }

        //public static bool SaveFeedback(List<DataModel.ClientData> listCD)
        //{
        //    try
        //    {
        //        var login = LoginDAL.CheckLoginByTempo(listCD.Take(1).SingleOrDefault().LoginName, listCD.Take(1).SingleOrDefault().LoginPwd, "SaveFeedback");

        //        if (login != null)
        //        {
        //            var context = new DataModel.GpsDCDataContext(CommonFx.GetConStr());
                    
        //            var settings = new XmlWriterSettings();
        //            settings.NewLineHandling = NewLineHandling.Entitize;
        //            settings.Indent = true;
        //            settings.OmitXmlDeclaration = true;

        //            var sb = new StringBuilder();

        //            using (var xw = XmlWriter.Create(sb, settings))
        //            {
        //                xw.WriteStartDocument();
        //                xw.WriteStartElement("DT");

                        

        //                xw.WriteEndDocument();
        //                xw.Close();
        //            }

        //            bool? IsErrorFound = true;
        //            string ErrorMessage = String.Empty;

        //            context.SP_UpdateDeliveryStatus_REST(XElement.Parse(sb.ToString()), null, null, "D2", ref IsErrorFound, ref ErrorMessage);

        //            if (IsErrorFound == false)
        //            {
        //                return true;
        //            }
        //            else
        //            {
        //                return false;
        //            }
        //        }
        //        else
        //        {
        //            return false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        var el = new DataModel.ErrorLog();
        //        el.ErrorMessage = ex.Message;
        //        el.ErrorForm = "DeliveryDAL";
        //        el.ErrorFunction = "SaveFeedback";
        //        CommonDAL.AddErrorLog(el);

        //        return false;
        //    }
        //}
    }
}